﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.FTPClient;
using System.Data.OracleClient;
using System.IO;
using WPFExcelReport.Helpers;
using INTEX.DataBase;
using INTEX.DataBase.Tools;
namespace WPFExcelReport
{
    public class ITXABPLANReport : BaseReport
    {
        public IntexReptServBLClient proxy;
        public OracleConnection OracleDB;
        string sheetName = "";

        public bool IsOnAuditing = false;
        public bool IsSendForAuditing = false;
        Dictionary<string, int> qpplan = new Dictionary<string, int>();
        public bool IsApproved = false;
        public FTPClient ftpClient;
        SETINTEXDB INTEXDB = new SETINTEXDB();
        INTEXDBC INTEXDBC;
        public ITXABPLANReport()
            : base()
        {

        }
        public ITXABPLANReport(Dictionary<string, object> dicParamater)
            : base(dicParamater)
        {
            strCompany = dicParamater["Company"].ToString().Trim();
            strExcelFile = dicParamater["ExcelFile"].ToString().Trim();
            UserID = (string)dicParamater["User"];
            Root = "C:\\IntexReport";
            INTEXDBC = new INTEXDBC(OracleDB, proxy);

        }
        public override void WriteExcelReport(string fileType)
        {
            try
            {
                exAgent.Company = "ITX";
                Helper.GetDate(ref date, ref year, ref month, ref day);
                strExcelFile = @"ITX\ITXABDOC.xls";
                Standard = "ABCOMPARE";
                INTEXDBC.OracleDB = OracleDB;
                INTEXDBC.proxy = proxy;
                #region=================检查路径======================
                destfolder = SetFolder(Root, "ITX");
                #endregion
                #region==========准备程序===================
                TemplatePath = Path.Combine(PathHelper.ApplicationPath, @"Report\" + strExcelFile);
                string ordernumer = date;
                exAgent.CheckWorkBook(ordernumer);
                #endregion
                ServerShareFile("", year, month, ordernumer, fileType);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        public void FillInExcelData(string tbname, int xStart = 1, int yStart = 0)
        {
            try
            {
                int x = xStart, y = yStart;
                DataTable dtSoruce = dsSource.Tables[tbname];
                DataTable dtOutput = new DataTable();
                int xcm=2;
                int rows=1;
                rows=dtSoruce.Rows.Count;
                foreach (DataColumn cm in dtSoruce.Columns)
                {
                    exAgent.SetCellValue(xcm, 1, cm.ColumnName);
                    /*for(int ir=0;ir<rows;ir++)
                    {
                        exAgent.SetCellValue(xcm, 2 + ir, dtSoruce.Rows[ir][cm.ColumnName].ToString());
                    }              */      
                    xcm++;
                } 
               exAgent.DataTableToExcel(dtSoruce, 2, 2,true);

                //int count = dtOutput.Rows.Count;
                //exAgent.SetCellValue(1, 1, 1);
                //exAgent.BatchConvert2Number(2, 6, count + 1, 7);
                //exAgent.BatchConvert2Number(2, 9, count + 1, 9);
                //exAgent.SetCellValue(1, 1, "采购单号");

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void ServerShareFile(string KRS, string year, string month, string ordernumer, string fileType)
        {
            try
            {
                string destfile, tempfile;
                ordernumer = "ABCOMPARE_" + ordernumer;

                filename = destfolder + "\\" + ordernumer + ".xls"; ;
                if (File.Exists(filename))
                {
                    File.Delete(filename);
                }
                destfile = filename;
                tempfile = CreateFile(ordernumer);
                if (File.Exists(destfile))
                {
                    File.Delete(destfile);
                }
                File.Move(tempfile, destfile);


                if (fileType == "OpenExcel")
                {
                    System.Diagnostics.Process.Start("EXCEL.EXE", filename);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private string CreateFile(string orderno)
        {
            string filename = "";
            try
            {
                string pwd = "";
                sheetName = orderno;
                exAgent.Open(TemplatePath);
                exAgent.Company = strCompany;
                exAgent.strExcelFile = strExcelFile;
                List<string> Standards = new List<string>();
                Standards.Add("ABCOMPARE");
                exAgent.CreateNewSheets(orderno + ".xls", Standards, pwd);
                exAgent.Save();
                filename = exAgent.FileName;
                foreach (DataTable tb in dsSource.Tables)
                {
                    if (tb.Rows.Count == 0)
                        continue; 
                    string tbName = tb.TableName;
                    exAgent.SetCurrentSheet("ABCOMPARE");
                    exAgent.CopySheet(tbName);
                    FillInExcelData(tbName, 0, 0);
                }
                exAgent.Save();
                exAgent.Close();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return filename;
        }
    }

}
